package lg.minecraft.plugin.cobblemoncore.config.mapping;

import com.cobblemon.mod.common.api.pokemon.PokemonSpecies;
import com.cobblemon.mod.common.pokemon.Species;
import lg.minecraft.plugin.cobblemoncore.LGCobblemonCore;
import lombok.Getter;

import java.util.Comparator;
import java.util.List;

/**
 * @author langle__
 * @version 1.0
 */
@Getter
public class SpeciesMapping extends AbstractMapping<Species> {

    private static final Comparator<Species> SPECIES_COMPARATOR = Comparator.comparingInt(Species::getNationalPokedexNumber);

    public SpeciesMapping() {
        super(LGCobblemonCore.getInstance(), "species.yml", LGCobblemonCore.getMappingDirectory().getPath());
    }

    @Override
    protected boolean isUseLocalFile() {
        return LGCobblemonCore.getMainConfig().isSpecies();
    }

    @Override
    protected void loadFromDynamicSource() {
        List<Species> speciesList = PokemonSpecies.getSpecies().stream().sorted(SPECIES_COMPARATOR).toList();
        for (Species species : speciesList) {
            String displayName = species.getTranslatedName().getString();
            set(species.showdownId(), displayName);
            mapping.put(displayName, species);
            reverse.put(species, displayName);
        }
    }

    @Override
    protected void loadFromLocalFile() {
        for (String key : getKeys(false)) {
            String value = getString(key);
            Species species = PokemonSpecies.getByName(key);
            if (species == null) {
                getPlugin().getLogger().warning("species: " + key + " not found!");
                continue;
            }
            mapping.put(value, species);
            reverse.put(species, value);
        }
    }

}

